package edu.gzy.rpsm_property.utils;

public class StringUtil {
    public static int getNumberFromString(String str){
        if (str == null || str.isEmpty()) {
            throw new IllegalArgumentException("输入字符串不能为空");
        }

        char[] chars = str.toCharArray();
        StringBuilder builder = new StringBuilder();
        for (char c : chars) {
            if (c >= '0' && c <= '9') {
                builder.append(c);
            }
        }

        // 检查 builder 是否为空
        if (builder.length() == 0) {
            throw new IllegalArgumentException("输入字符串不包含数字");
        }

        return Integer.valueOf(builder.toString());
    }

    public static String numberToLen2String(int i){
        if( -1 < i && i < 10){
            return "0" + i;
        }else{
            return i+"";
        }
    }
    //将价格字符串转换为以分为单位的整数
        public static Integer stringPriceToInt(String price) throws Exception{
            if(price != null){
                try{
                    Double d = Double.valueOf(price);
                    return Double.valueOf(d * 100).intValue();
                }catch (NumberFormatException e){
                    throw new Exception("不是浮点数格式");
                }
            }
            return null;
        }
}
